/**
 * Copyright 2012-2013 University Of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file sidebar-rule-properties.js
 * 
 * @desc Contains view and view controller functions for rule properties dialog box
 */


if (typeof OAA_AE_SIDEBAR === "undefined") {
  var OAA_AE_SIDEBAR = {};
}


if (typeof OAA_AE_SIDEBAR.util === "undefined") {
  OAA_AE_SIDEBAR.util = {};
}


/**
 * @function OAA_AE_SIDEBAR.initRuleInformation
 *
 * @desc Initializes rule properites dialog box
 */
 
OAA_AE_SIDEBAR.initRuleInformation = function () {

  var nls_strings = OAA_AE_SIDEBAR.nls_strings;

  OAA_AE_SIDEBAR.result_object = null;
  
  OAA_AE_SIDEBAR.rule_info_vbox        = window.document.getElementById("ID_VBOX_RULE_INFORMATION");
  OAA_AE_SIDEBAR.nls_rule_info_strings = window.document.getElementById("ID_STRINGBUNDLE_SIDEBAR_RULE_INFORMATION");
  
  OAA_AE_SIDEBAR.label_rule_info_title = document.getElementById('ID_LABEL_RULE_INFORMATION_TITLE');
  OAA_AE_SIDEBAR.button_hide_show      = document.getElementById('ID_BUTTON_HIDE_SHOW_RULE_INFORMATION');  
  OAA_AE_SIDEBAR.button_show_view      = document.getElementById('ID_BUTTON_SHOW_VIEW_INFORMATION');  

  OAA_AE_SIDEBAR.label_ruleset = document.getElementById('ID_LABEL_RULESET');    
  OAA_AE_SIDEBAR.label_level   = document.getElementById('ID_LABEL_LEVEL');  

  OAA_AE_SIDEBAR.menuitem_all_rulesets      = window.document.getElementById("ID_MENUITEM_ALL_RULESET_INFORMATION");
  OAA_AE_SIDEBAR.menuitem_current_ruleset   = window.document.getElementById("ID_MENUITEM_CURRENT_RULESET_INFORMATION");
  OAA_AE_SIDEBAR.menuitem_current_web_page  = window.document.getElementById("ID_MENUITEM_CURRENT_WEB_PAGE_INFORMATION");
  OAA_AE_SIDEBAR.menuitem_current_view      = window.document.getElementById("ID_MENUITEM_CURRENT_VIEW_INFORMATION");
  OAA_AE_SIDEBAR.menuitem_current_selection = window.document.getElementById("ID_MENUITEM_INFORMATION_CURRENT_SELECTION");

  OAA_AE_SIDEBAR.MSG_ALL_RULESET_INFO       = nls_strings.getString('allRulesetInfo');
  OAA_AE_SIDEBAR.MSG_ATTRIBUTE_INFO         = nls_strings.getString('attributeInfo');
  OAA_AE_SIDEBAR.MSG_AUTHOR                 = nls_strings.getString('author');
  OAA_AE_SIDEBAR.MSG_CURRENT_RULESET_INFO   = nls_strings.getString('currentRulesetInfo');
  OAA_AE_SIDEBAR.MSG_CURRENT_WEB_PAGE       = nls_strings.getString('currentPageInfo');
  OAA_AE_SIDEBAR.MSG_DEFINITION             = nls_strings.getString('definition');
  OAA_AE_SIDEBAR.MSG_DESCRIPTION            = nls_strings.getString('description');
  OAA_AE_SIDEBAR.MSG_ELEMENT                = nls_strings.getString('element');
  OAA_AE_SIDEBAR.MSG_ELEMENT_INFO           = nls_strings.getString('elementInfo');
  OAA_AE_SIDEBAR.MSG_ELEMENT_RESULT         = nls_strings.getString('elementResult');
  OAA_AE_SIDEBAR.MSG_INFORMATION            = nls_strings.getString('information');
  OAA_AE_SIDEBAR.MSG_INFORMATIONAL_LINKS    = nls_strings.getString('informationalLinks');
  OAA_AE_SIDEBAR.MSG_MANUAL_CHECKING_PROC   = nls_strings.getString('manualCheckingProc');
  OAA_AE_SIDEBAR.MSG_MORE_INFORMATION       = nls_strings.getString('moreInformation');
  OAA_AE_SIDEBAR.MSG_NLS_RULE_ID            = nls_strings.getString('nlsRuleId');
  OAA_AE_SIDEBAR.MSG_NO_INFORMATIONAL_LINKS = nls_strings.getString('noInformationalLinks');
  OAA_AE_SIDEBAR.MSG_NO_RESULT_PROPERTIES   = nls_strings.getString('noResultProperties');
  OAA_AE_SIDEBAR.MSG_NO_PURPOSE             = nls_strings.getString('noPurpose');
  OAA_AE_SIDEBAR.MSG_NO_RULE_RESULTS        = nls_strings.getString('noRuleResults');
  OAA_AE_SIDEBAR.MSG_NO_TECHNIQUES          = nls_strings.getString('noTechniques');
  OAA_AE_SIDEBAR.MSG_PURPOSE                = nls_strings.getString('purpose');
  OAA_AE_SIDEBAR.MSG_REQUIRED               = nls_strings.getString('required');
  OAA_AE_SIDEBAR.MSG_RAW_EVALUATION_RESULTS = nls_strings.getString('rawEvaluationResults');
  OAA_AE_SIDEBAR.MSG_RULESET                = nls_strings.getString('ruleset');
  OAA_AE_SIDEBAR.MSG_RULE                   = nls_strings.getString('rule');
  OAA_AE_SIDEBAR.MSG_RULE_CATEGORY          = nls_strings.getString('ruleCategory');
  OAA_AE_SIDEBAR.MSG_RULE_GROUP             = nls_strings.getString('ruleGroup');
  OAA_AE_SIDEBAR.MSG_RULE_TYPES             = nls_strings.getString('ruleTypes');
  OAA_AE_SIDEBAR.MSG_RULE_RESULTS           = nls_strings.getString('ruleResults');
  OAA_AE_SIDEBAR.MSG_RULE_DEFINITION        = nls_strings.getString('ruleDefinition');
  OAA_AE_SIDEBAR.MSG_RULE_SCOPE             = nls_strings.getString('ruleScope');
  OAA_AE_SIDEBAR.MSG_RULE_SUMMARY           = nls_strings.getString('ruleSummary');
  OAA_AE_SIDEBAR.MSG_RESULT_MESSAGE         = nls_strings.getString('resultMessage');
  OAA_AE_SIDEBAR.MSG_RESULT_PROPERTIES      = nls_strings.getString('resultProperties');
  OAA_AE_SIDEBAR.MSG_RULESET_ID             = nls_strings.getString('rulesetId');
  OAA_AE_SIDEBAR.MSG_SUMMARY_RESULTS        = nls_strings.getString('summaryResults');
  OAA_AE_SIDEBAR.MSG_TARGET_RESOURCES_DESCRIPTION = nls_strings.getString('targetResourcesDescription');
  OAA_AE_SIDEBAR.MSG_TARGET_RESOURCES_DETAILS     = nls_strings.getString('targetResourcesDetails');
  OAA_AE_SIDEBAR.MSG_TITLE             = nls_strings.getString('title');
  OAA_AE_SIDEBAR.MSG_TOOLTIP           = nls_strings.getString('tooltip');
  OAA_AE_SIDEBAR.MSG_TECHNIQUES        = nls_strings.getString('techniques');
  OAA_AE_SIDEBAR.MSG_URL               = nls_strings.getString('url');
  OAA_AE_SIDEBAR.MSG_VERSION           = nls_strings.getString('version');
  OAA_AE_SIDEBAR.MSG_WCAG20_REQ        = nls_strings.getString('wcag20req');

  OAA_AE_SIDEBAR.MSG_COUNT_VIOLATION     = nls_strings.getString('countViolation');
  OAA_AE_SIDEBAR.MSG_COUNT_VIOLATIONS    = nls_strings.getString('countViolations');
  OAA_AE_SIDEBAR.MSG_COUNT_WARNING       = nls_strings.getString('countWarning');
  OAA_AE_SIDEBAR.MSG_COUNT_WARNINGS      = nls_strings.getString('countWarnings');
  OAA_AE_SIDEBAR.MSG_COUNT_MANUAL_CHECK  = nls_strings.getString('countManualCheck');
  OAA_AE_SIDEBAR.MSG_COUNT_MANUAL_CHECKS = nls_strings.getString('countManualChecks');
  OAA_AE_SIDEBAR.MSG_COUNT_PASSED        = nls_strings.getString('countPassed');
  OAA_AE_SIDEBAR.MSG_COUNT_PERCENT_PASSED  = nls_strings.getString('countPercentPassed');
  OAA_AE_SIDEBAR.MSG_COUNT_HIDDEN        = nls_strings.getString('countHidden');
  
  OAA_AE_SIDEBAR.MSG_COUNT_REQ_RULE      = nls_strings.getString('countRequiredRule');
  OAA_AE_SIDEBAR.MSG_COUNT_REQ_RULES     = nls_strings.getString('countRequiredRules');

  OAA_AE_SIDEBAR.MSG_COUNT_REC_RULE      = nls_strings.getString('countRecommendedRule');
  OAA_AE_SIDEBAR.MSG_COUNT_REC_RULES     = nls_strings.getString('countRecommendedRules');

  OAA_AE_SIDEBAR.MSG_COUNT_BASIC_RULE    = nls_strings.getString('countBasicRule');
  OAA_AE_SIDEBAR.MSG_COUNT_BASIC_RULES   = nls_strings.getString('countBasicRules');

  OAA_AE_SIDEBAR.MSG_COUNT_TOTAL_RULE    = nls_strings.getString('countTotalRule');
  OAA_AE_SIDEBAR.MSG_COUNT_TOTAL_RULES   = nls_strings.getString('countTotalRules');

};

OAA_AE_SIDEBAR.INFORMATION_OPTION = {
  ALL_RULESETS      : 1,
  CURRENT_RULESET   : 2,
  CURRENT_WEB_PAGE  : 3,
  CURRENT_VIEW      : 4,
  CURRENT_SELECTION : 5
};


/**
 * @function OAA_AE_SIDEBAR.setInformationMenuOption
 *
 * @desc Set the option in the information options menu
 *
 * @param  {Number}  option  - Constant associated with the informatoin 
 */
 
OAA_AE_SIDEBAR.setInformationMenuOption = function (option) {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

//  OAA_AE_SIDEBAR.logger.debug("Info Option Menu: " + option);

  switch (option) {
  
  case IOM.ALL_RULESETS:
    OAA_AE_SIDEBAR.menuitem_all_rulesets.setAttribute('checked', 'true');
    break;
    
  case IOM.CURRENT_RULESET:
    OAA_AE_SIDEBAR.menuitem_current_ruleset.setAttribute('checked', 'true');
    break;

  case IOM.CURRENT_WEB_PAGE:
    OAA_AE_SIDEBAR.menuitem_current_web_page.setAttribute('checked', 'true');
    break;
    
  case IOM.CURRENT_VIEW:
    OAA_AE_SIDEBAR.menuitem_current_view.setAttribute('checked', 'true');
    break;
    
  default:
    OAA_AE_SIDEBAR.menuitem_current_selection.setAttribute('checked', 'true');
    break;
  
  }
  
};


/* ---------------------------------------------------------------- */
/*                    Rule Information                              */ 
/* ---------------------------------------------------------------- */

/**
 * @function OAA_AE_SIDEBAR.hideShowRuleInformation
 *
 * @memberOf OAA_WEB_ACCESSIBILITY_RULE_PROPS
 *
 * @desc Displays information about a ruleset
 *
 * param  {Boolean}  hide  - Optional, if false make information content visible
 */
 
OAA_AE_SIDEBAR.hideShowRuleInformation = function (hide) {

  if (typeof hide !== 'boolean') hide = true;

  OAA_AE_SIDEBAR.rule_info_vbox.hidden = hide && !OAA_AE_SIDEBAR.rule_info_vbox.hidden;
  
  var label_show = OAA_AE_SIDEBAR.nls_rule_info_strings.getString('showInformation');
  var label_hide = OAA_AE_SIDEBAR.nls_rule_info_strings.getString('hideInformation');
  
  var button = OAA_AE_SIDEBAR.button_hide_show;
  
  if (OAA_AE_SIDEBAR.rule_info_vbox.hidden) button.label = label_show;
  else button.label = label_hide;

};

/**
 * @function OAA_AE_SIDEBAR.clearInformationPanel
 *
 * @desc Clears the information panel of any content
 */
 
OAA_AE_SIDEBAR.clearInformationPanel = function () {

//  OAA_AE_SIDEBAR.logger.debug("Clear Rule Information");

  OAA_AE_SIDEBAR.result_object = null;

  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  
  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
  util.removeChildNodes(vbox);  
  
  util.highlightRuleInformation(false);
  
  util.addElement(vbox, "p", "no rule information", 'none');
  
  util.addEmptyContent(vbox);
  
//  OAA_AE_SIDEBAR.showCurrentRulesetInformation();
  
};

/**
 *@function OAA_AE_SIDEBAR.showNodeResultInformation
 *
 * @desc Displays information about the node result 
 * 
 * @param  {NodeResult}  node_result  - Node result object  
 * @param  {Boolean}     show         - Optional, if true force information 
 *                                      content to be visible
 */
 
OAA_AE_SIDEBAR.showNodeResultInformation = function (node_result, show) {

  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
 
  if (!node_result) return;

  OAA_AE_SIDEBAR.updateRulesetInformation();  
  
  var label = OAA_AE_SIDEBAR.label_rule_info_title;
    
  label.value =  OAA_AE_SIDEBAR.MSG_ELEMENT_RESULT + " " + OAA_AE_SIDEBAR.MSG_INFORMATION;
  
  OAA_AE_SIDEBAR.result_object = node_result;

  util.removeChildNodes(vbox);  
  
  // Rule Result Message

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RESULT_MESSAGE);
  util.addElement(vbox, "p", node_result.getResultMessage());

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RESULT_PROPERTIES);
  
  var props = node_result.getTargetResourceProperties();
  
  if (props.length) {
  
    var table_node = util.addElement(vbox, "table", "", "attributes");
    util.addPropertyHeader(table_node);
    for (var i = 0; i < props.length; i++) {
      var prop = props[i];
      var style = "even";
      if (i % 2) style = "odd"
      if ((prop.value !== "") && 
          (prop.value !== null) &&
          (typeof prop.value !== "undefined")) {
       util.addPropertyData(table_node, style, prop.label, prop.value, prop.description);
      }
      else {
       util.addPropertyData(table_node, style, prop.label, "undefined", prop.description);
      }
    }  
  }
  else {
    util.addElement(vbox, "p", OAA_AE_SIDEBAR.MSG_NO_RESULT_PROPERTIES);
  }

  OAA_AE_SIDEBAR.showRuleInformation(vbox, node_result);

  OAA_AE_SIDEBAR.setInformationMenuOption(OAA_AE_SIDEBAR.INFORMATION_OPTION.CURRENT_SELECTION);

  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

};

/**
 *@function OAA_AE_SIDEBAR.showElementInformation
 *
 * @desc Displays information about the rule results for an element
 * 
 * @param  {CacheItemResult}  cache_item_result  - Cache item result object  
 * @param  {Boolean}          show               - Optional, if true force information 
 *                                                 content to be visible
 */
 
OAA_AE_SIDEBAR.showElementInformation = function (cache_item_result, show) {

  if (!cache_item_result) return;
  
  if (typeof show !== 'boolean') show = false;
  
  OAA_AE_SIDEBAR.updateRulesetInformation();  
  
  var label = OAA_AE_SIDEBAR.label_rule_info_title;
    
  label.value =  OAA_AE_SIDEBAR.MSG_ELEMENT + " " + OAA_AE_SIDEBAR.MSG_ELEMENT;
  
  OAA_AE_SIDEBAR.result_object = cache_item_result;

  var cache_item = cache_item_result.cache_item;

//  OAA_AE_SIDEBAR.logger.debug("Update Element Information: " + cache_item);
  
  if (!cache_item) return;
  
  var de = cache_item.dom_element;

  if (!de) de = cache_item;
    
  if (de) {
  
    var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  
    var util = OAA_AE_SIDEBAR.util;
    var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
    util.removeChildNodes(vbox);  
    util.highlightRuleInformation(true);

    util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_ELEMENT);
    util.addElement(vbox, "p", cache_item.toString());

    var attributes = cache_item.getAttributes();

    util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RULE_RESULTS);

    if (cache_item_result.hasResults()) {

      OAA_AE_SIDEBAR.util.addNodeResults(vbox, 'Violations',    de.rules_violations,    'violation');
      OAA_AE_SIDEBAR.util.addNodeResults(vbox, 'Warnings',      de.rules_warnings,      'warning');
      OAA_AE_SIDEBAR.util.addNodeResults(vbox, 'Manual Checks', de.rules_manual_checks, 'manual_check');
      OAA_AE_SIDEBAR.util.addNodeResults(vbox, 'Passed',        de.rules_passed,        'passed');
      OAA_AE_SIDEBAR.util.addNodeResults(vbox, 'Hidden',        de.rules_hidden,        'hidden');  
    }
    else {
      util.addElement(vbox, "p", OAA_AE_SIDEBAR.MSG_NO_RULE_RESULTS);
    } 
  }
  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_ATTRIBUTE_INFO);

  OAA_AE_SIDEBAR.util.labelsValuesToTable(vbox, cache_item_result.cache_item.getAttributes());

  OAA_AE_SIDEBAR.setInformationMenuOption(OAA_AE_SIDEBAR.INFORMATION_OPTION.CURRENT_VIEW);

  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

};

/**
 * @function OAA_AE_SIDEBAR.showRuleGroupInformation
 *
 * @desc Displays information about a group of rules
 *
 * @param  {Group Object}  group - Rule group object
 *
 * @param  {Boolean}       show  - Optional, if true force the group information
 *                                 to be visible
 */
 
OAA_AE_SIDEBAR.showRuleGroupInformation = function (group, show) {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

//  OAA_AE_SIDEBAR.logger.debug("  GROUP INFO: " + group + "  " + group.title)

  if (typeof group !== 'object') return;

  if (typeof show !== 'boolean') show = false;

  OAA_AE_SIDEBAR.updateRulesetInformation();  

  OAA_AE_SIDEBAR.result_object = group_information;

  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  
  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  var table_node;
  
  util.removeChildNodes(vbox);  
  util.highlightRuleInformation(true);
  
  var label = OAA_AE_SIDEBAR.label_rule_info_title;
    
  label.value =  OAA_AE_SIDEBAR.MSG_RULE_GROUP + ' ' + OAA_AE_SIDEBAR.MSG_INFORMATION;

  var group_information = group.getRuleResultsGroupInfo();

  var title = group_information.title;
  var url   = group_information.url;
  var desc  = group_information.description;

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_TITLE);
  util.addElement(vbox, "p", title);

  if ((desc !== '') || (url  !== '')) {
    util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_DEFINITION);
    if (desc !== '') util.addElement(vbox, "p",  desc , "", "");
    if (url  !== '') util.addElement(vbox, "p",  OAA_AE_SIDEBAR.MSG_MORE_INFORMATION + " " + title, "more_info", url);
  }  

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RULE_TYPES);
  table_node = util.addElement(vbox, "table", "", "group", "presentation");
  util.addCount(table_node, group_information.required_rules,    "required");
  util.addCount(table_node, group_information.recommended_rules, "recommended");
  
  var rs = group.getNodeResultSummary();

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_SUMMARY_RESULTS);
  table_node = util.addElement(vbox, "table", "", "group", "presentation");
  util.addCount(table_node, rs.passed,        "passed");
  util.addCount(table_node, rs.manual_checks, "manual_check");
  util.addCount(table_node, rs.violations,    "violation");  
  util.addCount(table_node, rs.warnings,      "warning");
  util.addCount(table_node, rs.hidden,        "hidden");

  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

};

/**
 * @function OAA_AE_SIDEBAR.showRuleInformation
 *
 * @desc Shows properites of a rule or rule result object 
 *
 * @param {DOM Node}                    node  - DOM node to append rule information    
 * @param {Rule or Rule Result Object}  rule  - Rule or rule result object    
 */
 
OAA_AE_SIDEBAR.showRuleInformation = function (node, rule) {

  var util = OAA_AE_SIDEBAR.util;

    // Description

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_RULE_DEFINITION);
    util.addElement(node, "p", rule.getRuleDefinition());
 
   // Add rule scope

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_RULE_SCOPE);
    util.addElement(node, "p", rule.getRuleScope(), "");
    
    // Rule Summary
    
    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_RULE_SUMMARY);
    util.addElement(node, "p", rule.getRuleSummary());

    // Required or Recommended
    
    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_REQUIRED);
    util.addElement(node, "p", rule.getRuleRequiredYesNo() + ", " + rule.getRuleRequiredOrRecommended());
   
    // NLS Rule Category

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_RULE_CATEGORY);
    util.addElement(node, "p", rule.getRuleCategory().title);

    // NLS Rule Identification

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_NLS_RULE_ID);
    util.addElement(node, "p", rule.getRuleIdNLS());

 
    // Purpose

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_PURPOSE);
    var ul_node = util.addElement(node, "ul", "", "purpose");

    var purpose = rule.getPurpose();

    if (purpose && purpose.length) { 

      for (var i = 0; i < purpose.length; i++ ) {
        util.addElement(ul_node, 'li', purpose[i]);
      } // end loop
      
    } else {  
      util.addElement(ul_node, OAA_AE_SIDEBAR.MSG_NO_PURPOSE, 'none');
    }

    // WCAG 2.0 Requirements
    
    var primary = rule.getPrimarySuccessCriterion();

    var related = rule.getRelatedSuccessCriteria();

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_WCAG20_REQ);
    ul_node = util.addElement(node, "ul", "", "requirements");

    util.addElement(ul_node, "li", (primary.title + " Primary"), 'primary', primary.url);
    
    for (i = 0; i < related.length; i++ ) {
      var r = related[i];       
      util.addElement(ul_node, "li", r.title, 'related', r.url);
    } // end loop

    // Add techniques

    var techniques = rule.getTechniques();

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_TECHNIQUES);
    ul_node = util.addElement(node, "ul", "", "techniques");

    if (techniques && techniques.length) { 

      for (i = 0; i < techniques.length; i++ ) {
        var t = techniques[i];
        if (t.url.length) util.addElement(ul_node, "li", t.title, "", t.url);
        else util.addElement(ul_node, "li", t.title);
      } // end loop
      
    } else {  
      util.addElement(ul_node, "li", OAA_AE_SIDEBAR.MSG_NO_TECHNIQUES, 'none');
    }

    // Add manual checks

    var manual_checks = rule.getManualCheckProcedures();

    if (manual_checks && manual_checks.length) {
    
      util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_MANUAL_CHECKING_PROC);
      ul_node = util.addElement(node, "ul", "", "manual_checks");

      for (i = 0; i < manual_checks.length; i++ ) {
        var mc = manual_checks[i];
        if (mc.url.length) util.addElement(ul_node, "li", mc.title, "", mc.url);
        else util.addElement(ul_node, "li", mc.title);
      } // end loop
    }  
    // Add informational links

    var info_links = rule.getInformationalLinks('text');

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_INFORMATIONAL_LINKS);
    ul_node = util.addElement(node, "ul", "", "informational");

    if (info_links && info_links.length) { 

      for (i = 0; i < info_links.length; i++ ) {
        util.addElement(ul_node, "li", info_links[i].title, info_links[i].style, info_links[i].url);
      } // end loop
      
    } else {  
      util.addElement(ul_node, "li", OAA_AE_SIDEBAR.MSG_NO_INFORMATIONAL_LINKS, 'none');
    }


    // Add target resource information

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_TARGET_RESOURCES_DESCRIPTION);
    util.addElement(node, "p", rule.getTargetResourcesDescription(), "");

    util.addElement(node, "h2", OAA_AE_SIDEBAR.MSG_TARGET_RESOURCES_DETAILS);
    ul_node = util.addElement(node, "ul", "", "informational");

    var tr = rule.getTargetResources()

    if (tr) { 

      for (i = 0; i < tr.length; i++ ) {
        util.addElement(ul_node, "li", tr[i], "");
      } // end loop
      
    } else {  
      util.addElement(ul_node, "li", OAA_AE_SIDEBAR.MSG_NO_INFORMATIONAL_LINKS, 'none');
    }

};

/**
 * @function OAA_AE_SIDEBAR.showRuleResultInformation
 *
 * @desc Shows the rule properites of a node result
 *
 * @param {Result Object}  result_object  - Node, rule or filtered rule result object    
 * @param {boolean}        show           -  Optional, if true force information to be visible
 */
 
OAA_AE_SIDEBAR.showRuleResultInformation = function (result_object, show) {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  var rule;
  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  var wcag20_nls = OpenAjax.a11y.wcag20_nls.getNLS();

  if (typeof result_object !== 'object') return;

  if (typeof show !== 'boolean') show = false;

  OAA_AE_SIDEBAR.updateRulesetInformation();  

//  OAA_AE_SIDEBAR.logger.debug("Update Rule Information: " + result_object);

  OAA_AE_SIDEBAR.result_object = result_object;

  if (result_object) {
  
    OAA_AE_SIDEBAR.util.highlightRuleInformation(true);

    var util = OAA_AE_SIDEBAR.util;
    
    var rule_nummber = 0;
    if (typeof result_object.rule_number === 'number') rule_number = result_object.rule_number;
        
    var label = OAA_AE_SIDEBAR.label_rule_info_title;
    
    label.value =  OAA_AE_SIDEBAR.MSG_RULE + ' ' + OAA_AE_SIDEBAR.MSG_INFORMATION;

    var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
    util.removeChildNodes(vbox);  
    util.highlightRuleInformation(true);

    // Rule Result Message

    util.addElement(vbox, "h2", "Rule Result Value");
    util.addElement(vbox, "p", result_object.getRuleResultValueNLS() + " (" + result_object.getRuleResultValue().toString() + ")");

    util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RESULT_MESSAGE);
    
    
    var ul_node = util.addElement(vbox, "ul", "");
    
    var rms = result_object.getResultMessages()
    
    for (var i = 0; i < rms.length; i++) {
    
      util.addElement(ul_node, "li", rms[i]);
      
    }  

    
    OAA_AE_SIDEBAR.showRuleInformation(vbox, result_object);
    
  }
  else {
    OAA_AE_SIDEBAR.clearInformationPanel();
  }

  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

};

/**
 * @function OAA_AE_SIDEBAR.addRulesetInformation
 *
 * @desc Displays information about a ruleset
 * 
 * @param {Object}  node     -  Dom to add the information
 * @param {Object}  ruleset_information  -  A RulesetInformation object
 * @param {String}  heading  -  Optional tag name for heading
 */
 
OAA_AE_SIDEBAR.addRulesetInformation = function (node, ruleset_information, heading) {

  if (typeof heading !== 'string' || 
      (heading.length === 0 ) ||
      (heading[0].toLowerCase() !== 'h')) heading = "h2";

  var util = OAA_AE_SIDEBAR.util;

  util.addElement(node, heading, OAA_AE_SIDEBAR.MSG_VERSION);
  util.addElement(node, "p",  ruleset_information.version, "");

  util.addElement(node, heading, OAA_AE_SIDEBAR.MSG_TOOLTIP);
  util.addElement(node, "p",  ruleset_information.tooltip, "");

  util.addElement(node, heading, OAA_AE_SIDEBAR.MSG_DESCRIPTION);
  util.addElement(node, "p",  ruleset_information.description, "");

  util.addElement(node, heading, OAA_AE_SIDEBAR.MSG_AUTHOR);
  util.addElement(node, "p",  ruleset_information.author, "", ruleset_information.author_url);

  util.addElement(node, heading,  OAA_AE_SIDEBAR.MSG_RULE_TYPES);
  var node_table = util.addElement(node, "table", "", "group", "presentation");
  util.addCount(node_table, ruleset_information.required_rules,   "required");
  util.addCount(node_table, ruleset_information.recommended_rules, "recommended");
  util.addCount(node_table, ruleset_information.total_rules,       "total");

  util.addElement(node, heading,  OAA_AE_SIDEBAR.MSG_RULESET_ID);
  util.addElement(node, "p", ruleset_information.id, "");

};

/**
 * @function OAA_AE_SIDEBAR.updateRulesetInformation
 *
 * @desc Updates current ruleset information
 */
 
OAA_AE_SIDEBAR.updateRulesetInformation = function () {

  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  var nls_wcag20  = OpenAjax.a11y.wcag20_nls.getNLS();
  
  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
  var prefs   = OAA_WEB_ACCESSIBILITY_PREF.util.getPreferences();
  var ruleset = OpenAjax.a11y.all_rulesets.getRuleset(prefs.ruleset_id);
  
  OAA_AE_SIDEBAR.label_ruleset.value = ruleset.ruleset_title;  
  OAA_AE_SIDEBAR.label_level.value   = nls_wcag20.getEvaluationLevelsNLS(prefs.evaluation_levels);
  
};



/**
 * @function OAA_AE_SIDEBAR.showCurrentRulesetInformation
 *
 * @desc Shows current ruleset informatiom
 *
 * @param  {Boolean}  show  - Optional, if true force information to be visible
 */
 
OAA_AE_SIDEBAR.showCurrentRulesetInformation = function (show) {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  if (typeof show !== 'boolean') { 
    show = false;
  }
  
  if (typeof show === 'boolean' && show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  var prefs   = OAA_WEB_ACCESSIBILITY_PREF.util.getPreferences();
  var ruleset = OpenAjax.a11y.all_rulesets.getRuleset(prefs.ruleset_id);
  
  OAA_AE_SIDEBAR.updateRulesetInformation();  
  
  util.removeChildNodes(vbox);  
  util.highlightRuleInformation(true);
  
  var ruleset_information = ruleset.getRulesetInfo();

  OAA_AE_SIDEBAR.label_rule_info_title.value = OAA_AE_SIDEBAR.MSG_CURRENT_RULESET_INFO;
  
  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_TITLE);
  util.addElement(vbox, "p",  ruleset_information.title, "");  

  OAA_AE_SIDEBAR.addRulesetInformation(vbox, ruleset_information, "h2");
  
  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

  OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_RULESET);

};


/**
 * @function OAA_AE_SIDEBAR.showAllRulesetInformation
 *
 * @desc Shows information on all the currently installed rulesets
 *
 * @param  {Boolean}  show  - Optional, if true or undefined show information
 */
 
OAA_AE_SIDEBAR.showAllRulesetInformation = function (show) {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  if (typeof show !== 'boolean') show = true; 

  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
  
  var nls_strings = OAA_AE_SIDEBAR.nls_rule_info_strings;
  
  OAA_AE_SIDEBAR.updateRulesetInformation();  

  util.removeChildNodes(vbox);  
  util.highlightRuleInformation(true);

  OAA_AE_SIDEBAR.label_rule_info_title.value = OAA_AE_SIDEBAR.MSG_ALL_RULESET_INFO;

  var rulesets = OpenAjax.a11y.all_rulesets.getAllRulesets();
  
//  OAA_AE_SIDEBAR.logger.debug("Number of rulesets: " + rulesets.length);  

  var rulesets_len = rulesets.length;

  var j = 1;
  for (var i = 0; i < rulesets_len; i++) {
  
    var ruleset_information = rulesets[i].getRulesetInfo();

//    OAA_AE_SIDEBAR.logger.debug("  Ruleset " + i + ": " + ruleset.getRulesetTitle());  

    var title = this.MSG_RULESET + " " + (i+1) + ": " + ruleset_information.title;

    util.addElement(vbox, "h2",  title, "");

    OAA_AE_SIDEBAR.addRulesetInformation(vbox, ruleset_information, "h3");
    
    if (j++ < rulesets_len) OAA_AE_SIDEBAR.util.addSeparator(vbox); 
    
  }  
  
  if (show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);
  
  OAA_AE_SIDEBAR.setInformationMenuOption(IOM.ALL_RULESETS);
  
};

/**
 * @function OAA_AE_SIDEBAR.showViewInformation
 *
 * @desc Update the sidebar content with  information on the current view or selection
 *
 */

OAA_AE_SIDEBAR.showViewInformation = function () {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  var node_deck = window.document.getElementById('ID_DECK_VIEW');
  var nd_index  = node_deck.selectedIndex;
    
  switch (OAA_AE_SIDEBAR.view.type) {

  case 'element':
  
    var cir = OAA_AE_SIDEBAR.ElementView.cache_item_result;
 
    if ((nd_index === 3) && cir) OAA_AE_SIDEBAR.showElementInformation(cir, true);
    else OAA_AE_SIDEBAR.showCurrentRulesetInformation(true);
    
    OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_VIEW);
  
    break;

  case 'summary':
  case 'category':
  case 'guideline':
  case 'principle':
  case 'success_criterion':
    var frc  = OAA_AE_SIDEBAR.RuleCategoryView.filtered_rule_category;
    var rri  = OAA_AE_SIDEBAR.RuleCategoryView.rule_result_item;

    if (nd_index === 1) OAA_AE_SIDEBAR.showRuleResultInformation(rri, true);
    else OAA_AE_SIDEBAR.showRuleGroupInformation(frc, true);

    OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_VIEW);
   
    break;

  default:
    break;
    
  }  

};

/**
 * @function OAA_AE_SIDEBAR.showCurrentPageInformation
 *
 * @desc Update the sidebar content with information on the cweb page
 *
 * @param  {Boolean}  show  - Optional, if true or undefined show information
 */

OAA_AE_SIDEBAR.showCurrentPageInformation = function (show) {

  function addElementInfo(node, style, name, v, h, t) {

    OAA_AE_SIDEBAR.logger.debug("  Element Info: " + name + " " + v + " " + h + " " + t);  

    var node_tr = util.addElement(node, "tr", "", style);
    util.addElement(node_tr, "th", name, "");
    
    if (v) util.addElement(node_tr, "td", v.toString(), "number");
    else util.addElement(node_tr, "td", "0", "zero");

    if (h) util.addElement(node_tr, "td", h.toString(), "number");
    else util.addElement(node_tr, "td", "0", "zero");
    
    if (t) util.addElement(node_tr, "td", t.toString(), "number");
    else util.addElement(node_tr, "td", "0", "zero");

  }
  
  
  
  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  if (typeof show !== 'boolean') { 
    show = false;
  }
  
  if (typeof show === 'boolean' && show) OAA_AE_SIDEBAR.hideShowRuleInformation(false);

  var util = OAA_AE_SIDEBAR.util;
  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;
    
  util.removeChildNodes(vbox);  
  util.highlightRuleInformation(true);
  
  var wpi = OAA_AE_SIDEBAR.evaluation_result.getPageInformation();

  var label = OAA_AE_SIDEBAR.label_rule_info_title;
    
  label.value =  OAA_AE_SIDEBAR.MSG_CURRENT_WEB_PAGE;

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_TITLE);
  util.addElement(vbox, "p",  wpi.title, "");  

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_URL);
  util.addElement(vbox, "p",  wpi.url, "");  
  
  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_ELEMENT_INFO);
  var node_table = util.addElement(vbox, "table", "", "attributes");
  var node_tr    = util.addElement(node_table, "tr", "", "header");
  util.addElement(node_tr, "th", "Type of Element", "");
  util.addElement(node_tr, "th", "Visible", "");
  util.addElement(node_tr, "th", "Hidden", "");
  util.addElement(node_tr, "th", "Total", "");
 
  var e  = wpi.elements;
  var ve = wpi.visible_elements;
  var he = wpi.hidden_elements;
 
  addElementInfo(node_table, "odd",  "Audio",         ve.audio,         he.audio,         e.audio);
  addElementInfo(node_table, "even", "Form Controls", ve.form_controls, he.form_controls, e.form_controls);

  addElementInfo(node_table, "odd",  "Elements with events", ve.elements_with_events, he.elements_with_events, e.elements_with_events);
  addElementInfo(node_table, "even", "Headings (h1-h6)",     ve.headings,             he.headings,             e.headings);

  addElementInfo(node_table, "odd",  "iframes",   ve.iframes,    he.iframes,   e.iframes);
  addElementInfo(node_table, "even", "Images",    ve.images,     he.images,    e.images);

  addElementInfo(node_table, "odd",  "Landmarks", ve.landmarks,  he.landmarks, e.landmarks);
  addElementInfo(node_table, "even", "Links",     ve.links,      he.links,     e.links);

  addElementInfo(node_table, "odd",  "Lists",      ve.lists,     he.lists,     e.lists);
  addElementInfo(node_table, "even", "List Items", ve.listitems, he.listitems, e.listitems);

  addElementInfo(node_table, "odd",  "Objects",    ve.objects,   he.objects,   e.objects);
  addElementInfo(node_table, "even", "Tables",     ve.tables,    he.tables,    e.tables);

  addElementInfo(node_table, "odd",  "Video",      ve.video,     he.video,     e.video);
  addElementInfo(node_table, "even", "Widgets",    ve.widgets,   he.widgets,   e.widgets);
  
  addElementInfo(node_table, "odd",  "All Elements", ve.total,  he.total,     e.total);

  var rs = OAA_AE_SIDEBAR.evaluation_result.getNodeResultSummary();

  util.addElement(vbox, "h2", OAA_AE_SIDEBAR.MSG_RAW_EVALUATION_RESULTS);
  table_node = util.addElement(vbox, "table", "", "group", "presentation");
  util.addCount(table_node, rs.percent_passed,  "percent_passed");
  util.addCount(table_node, rs.passed,          "passed");
  util.addCount(table_node, rs.manual_checks,   "manual_check");
  util.addCount(table_node, rs.violations,      "violation");  
  util.addCount(table_node, rs.warnings,        "warning");
  util.addCount(table_node, rs.hidden,          "hidden");


  OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_WEB_PAGE);

};


/**
 * @function OAA_AE_SIDEBAR.showSelectionInformation
 *
 * @desc Update the sidebar content with information on the current view or selection
 *
 */

OAA_AE_SIDEBAR.showSelectedInformation = function () {

  var IOM = OAA_AE_SIDEBAR.INFORMATION_OPTION;

  var node_deck = window.document.getElementById('ID_DECK_VIEW');
  var nd_index  = node_deck.selectedIndex;
  var nr;
    
  switch (OAA_AE_SIDEBAR.view.type) {

  case 'element':
  
    nr  = OAA_AE_SIDEBAR.ElementResultView.node_result;
    var cir = OAA_AE_SIDEBAR.ElementView.cache_item_result;
 
    if ((nd_index === 3) && nr) {
      OAA_AE_SIDEBAR.showNodeResultInformation(nr, true);
    }
    else {
      if (cir) OAA_AE_SIDEBAR.showElementInformation(cir, true);
      else OAA_AE_SIDEBAR.showCurrentRulesetInformation(true);
    }
    
    OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_SELECTED);
  
    break;

  case 'summary':
  case 'category':
  case 'guideline':
  case 'principle':
  case 'success_criterion':
    nr = OAA_AE_SIDEBAR.RuleResultView.node_result;
    var frc  = OAA_AE_SIDEBAR.RuleCategoryView.filtered_rule_category;
    var rrgi = OAA_AE_SIDEBAR.RuleCategoryView.rule_results_group_item;
    var rri  = OAA_AE_SIDEBAR.RuleCategoryView.rule_result_item;

    if ((nd_index === 1) && nr) {
       OAA_AE_SIDEBAR.showNodeResultInformation(nr, true);
    }
    else {
    
      if (rri) OAA_AE_SIDEBAR.showRuleResultInformation(rri, true);
      else if (rrgi) OAA_AE_SIDEBAR.showRuleGroupInformation(rrgi, true);
      else OAA_AE_SIDEBAR.showRuleGroupInformation(frc, true);
    
    }
    
    OAA_AE_SIDEBAR.setInformationMenuOption(IOM.CURRENT_SELECTED);
   
    break;

  default:
    break;
    
  }  

};


/**
 * @function OAA_AE_SIDEBAR.showViewInformationButton
 *
 * @desc Update the sidebar content with  information on the current view or selection
 *
 */

OAA_AE_SIDEBAR.pressViewInformationButton = function() {

  var show_view   = OAA_AE_SIDEBAR.nls_rule_info_strings.getString('showView');

  if (OAA_AE_SIDEBAR.button_show_view.label === show_view) OAA_AE_SIDEBAR.showViewInformation();
  else OAA_AE_SIDEBAR.showSelectedInformation();
  
  // if info hidden force into view
  OAA_AE_SIDEBAR.hideShowRuleInformation(false);
  
};


/**
 * @function OAA_AE_SIDEBAR.util..highlightRuleInformation
 *
 * @desc Highlights of dims the rule information box
 *
 * @param {Boolean}  enable     - ture if rule information, false if none  
 */

OAA_AE_SIDEBAR.util.highlightRuleInformation = function (enable) {

  var vbox = OAA_AE_SIDEBAR.rule_info_vbox;

  if ((typeof enable === "boolean") && !enable) {
    vbox.style.backgroundColor = "#DDDDDD";
    vbox.style.color = "gray";
  }
  else {
    vbox.style.backgroundColor = "#F8F8F8";
    vbox.style.color = "black";  
  }

};

/**
 * @function OAA_AE_SIDEBAR.util.removeChildNodes
 *
 * @desc Removes all the children of an DOM element node
 *
 * @param {DOM node}  node  - DOM Element node    
 */

OAA_AE_SIDEBAR.util.removeChildNodes = function (node) {

  if (!node) return;

  while(node.firstChild) {
    node.removeChild(node.firstChild);
  }
  
};

/**
 * @function OAA_AE_SIDEBAR.util.addElement
 *
 * @desc Creates a LI element containing a DOM Text Node and appends it to a list element
 *
 * @param {DOM node}  node       - DOM Element node    
 * @param {String}  element      - element tag      
 * @param {String}  style        - class name     
 * @param {String}  text         - Text content to add to a DOM element node     
 * @param {String}  url_or_pres  - optional URL for a link or presentation role for element 
 *
 * @return {Object}  Returns the DOM node of the ceated element
 */

OAA_AE_SIDEBAR.util.addElement = function (node, element, text, style, url_or_pres) {

  var text_node;

  if (!node || (typeof element !== 'string') || (typeof text !== 'string')) return null;

  var doc = window.document;

  var element_node = doc.createElementNS("http://www.w3.org/1999/xhtml", element);
  
  if ((typeof style === 'string') && style.length) element_node.setAttribute("class", style);
 
  if ((typeof text === 'string') && text.length) {
    text_node = doc.createTextNode(text);
  
    if ((typeof url_or_pres === 'string') && url_or_pres.length && url_or_pres !== 'presentation') {

      var link_node = doc.createElementNS("http://www.w3.org/1999/xhtml","a");
      link_node.setAttribute('href', url_or_pres);
      link_node.setAttribute('target', "_rule_properties");

      link_node.appendChild(text_node);
      element_node.appendChild(link_node);
    }
    else {
      element_node.appendChild(text_node);
      if (url_or_pres !== 'presentation') element_node.setAttribute('role', 'presentation');
    }
  }  
  
  node.appendChild(element_node);

  return element_node;
};


/**
 * @function OAA_AE_SIDEBAR.util.addEmptyContent
 *
 * @desc Creates empty content to support uniform spacing
 *
 * @param {DOM node}  node  - DOM Element node    
 * @param {Number}    n     - Number of lines of emty content (optional)    
 *
 */

OAA_AE_SIDEBAR.util.addEmptyContent = function (node, n) {

  if (!node) return;

  if (typeof n !== 'number' || n === 0) n = 15;

  var doc = window.document;

  for (var i = 0; i < n; i++ ) {

    var element_node = doc.createElementNS("http://www.w3.org/1999/xhtml", "p");
    element_node.setAttribute('style', 'visibility: hidden');
    
    var text_node = doc.createTextNode("x");
    element_node.appendChild(text_node)

    node.appendChild(element_node);
  
  }
  
};

/**
 * @function OAA_AE_SIDEBAR.util.addSeparator
 *
 * @desc Creates separator
 *
 * @param {DOM node}  node  - DOM Element node    
 */

OAA_AE_SIDEBAR.util.addSeparator = function (node) {

  if (!node) return;

  var element_node = window.document.createElementNS("http://www.w3.org/1999/xhtml", "hr");

  node.appendChild(element_node);
  
};


/**
 * @function util.addTextNode
 *
 * @memberOf OAA_AE_SIDEBAR
 *
 * @desc Creates a DOM Text Node and appends it to the element node
 *
 * @param {DOM node}  node  - DOM Element node    
 * @param {String}    text  - Text content to add to a DOM element node     
 */

OAA_AE_SIDEBAR.util.addTextNode = function (node, text) {

  var text_node = document.createTextNode(text);  
  node.appendChild(text_node);

};

/**
 * @function addTableRowLabelValue
 *
 * @memberOf OAA_AE_SIDEBAR.util.
 *
 * @desc Adds a table row consisting of one TH and one TD element, the TD element can be styled 
 *
 * @param {DOM node}  node        -  DOM node of a tbody or table element to add row   
 * @param {String}    tr_style    -  Style class name to be assigned to the TR element  
 * @param {String}    label       -  Text content for the TH element of the row  
 * @param {String}    label_style -  Style class name to be assigned to the TH element  
 * @param {String}    value       -  Text content for the TD element of the row
 * @param {String}    value_style -  Style class name to be assigned to the TD element  
 */
 
OAA_AE_SIDEBAR.util.addTableRowLabelValue = function (node, tr_style, label, label_style, value, value_style) {

  var tr = document.createElementNS("http://www.w3.org/1999/xhtml","tr");
  if (tr_style && tr_style.length) tr.setAttribute('class', tr_style);
  
  var th = document.createElementNS("http://www.w3.org/1999/xhtml","th"); 
  if (label_style && label_style.length) th.setAttribute('class', label_style);
  var text = document.createTextNode(label);
  th.appendChild(text);
  tr.appendChild(th);
  
  var td = document.createElementNS("http://www.w3.org/1999/xhtml","td");
  if (value_style && value_style.length) td.setAttribute('class', value_style);
  text = document.createTextNode(value);
  td.appendChild(text);
  tr.appendChild(td);
  
  node.appendChild(tr);
  
};

/**
 * @function addTableRowNone
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds a table row consisting of one TH and one TD element, the TD element can be styled 
 *
 * @param {DOM node}  node     -  DOM node of a tbody or table element to add row   
 * @param {String}    message  -  Message describing the empty table  
 * @param {number}    cols     -  Number of columns to span  
 */
 
OAA_AE_SIDEBAR.util.addTableRowNone = function (node, message, cols) {

  var tr = document.createElementNS("http://www.w3.org/1999/xhtml", "tr");
  tr.setAttribute('class', 'odd');
  
  var th = document.createElementNS("http://www.w3.org/1999/xhtml", "th"); 
  th.setAttribute('class', 'none');
  th.setAttribute('colspan', cols);
  
  var text = document.createTextNode(message);
  th.appendChild(text);
  tr.appendChild(th);
  
  node.appendChild(tr);
  
};

/**
 * @function labelsValuesToTable
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds an array of objects with 'label' and 'value' properties to a HTML table 
 *
 * @param {DOM node}  node              -  DOM node of a tbody or table element to add row   
 * @param {Array}     labels_and_values -  Array of label/value objects to insert into a table 
 */
 
OAA_AE_SIDEBAR.util.labelsValuesToTable = function (node, labels_and_values) {

  if (!node || !labels_and_values || !labels_and_values.length) return;
  var util = OAA_AE_SIDEBAR.util;

  var node_table = OAA_AE_SIDEBAR.util.addElement(node, 'table', '', 'attributes', '');
  node_table.setAttribute('summary', 'attributes and their values for an dom element node');
  
  var node_thead = OAA_AE_SIDEBAR.util.addElement(node_table, 'thead', '', '', '');
  var node_tr    = OAA_AE_SIDEBAR.util.addElement(node_thead, 'tr', '', '', '');
  OAA_AE_SIDEBAR.util.addElement(node_tr, 'th', 'Attribute', '', '');
  OAA_AE_SIDEBAR.util.addElement(node_tr, 'th', 'Value', '', '');
  
  var node_tbody = OAA_AE_SIDEBAR.util.addElement(node_table, 'tbody', '', '', '');
  
  var labels_and_values_len = labels_and_values.length;

  for (var i = 0; i < labels_and_values_len; i++) {
  
     var lv = labels_and_values[i];

     if (i % 2) util.addTableRowLabelValue(node_tbody, 'odd', lv.label, '', lv.value, '');
     else util.addTableRowLabelValue(node_tbody, 'even', lv.label, '', lv.value, '');

  } // end loop

};

/**
 * @function addNodeResults
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds a header and list of rule result messages from node results array 
 *
 * @param {String}  label        - Label for node results   
 * @param {Array}   node_results - Array of node result objects 
 */
 
OAA_AE_SIDEBAR.util.addNodeResults = function (node, label, node_results, style) {

  if (typeof style !== 'string') style = "";

  if (node_results.length > 0) {
    OAA_AE_SIDEBAR.util.addElement(node, 'h3', label, style, '');
    var node_ul = OAA_AE_SIDEBAR.util.addElement(node, 'ul', '', '', '');
    
    for (var i = 0; i < node_results.length; i++) {
      OAA_AE_SIDEBAR.util.addElement(node, 'li', node_results[i].getResultMessage(), '', '');
    }  
  }

};

/**
 * @function addCount
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds a list item describing a count result
 *
 * @param {Object}  node   - DOM node to add the results   
 * @param {Number}  count  - Number of results 
 * @param {String}  type   - Type of results 
 */
 
OAA_AE_SIDEBAR.util.addCount = function (node, count, type) {

  function getMessage(count, singular, plural) {
  
    var str = " " + plural;
    
    if (count === 1) str = " " + singular;
    
    return str;
  }
  
  var util = OAA_AE_SIDEBAR.util;

  var str = "";
  var node_tr = util.addElement(node, 'tr', "", "presentation");
  var style = (count === 0) ? 'zero' : type;

  switch (type) {
  
  case 'passed':
    str = OAA_AE_SIDEBAR.MSG_COUNT_PASSED;
    break;

  case 'percent_passed':
    str = OAA_AE_SIDEBAR.MSG_COUNT_PERCENT_PASSED;
    break;

  case "manual_check":
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_MANUAL_CHECK, OAA_AE_SIDEBAR.MSG_COUNT_MANUAL_CHECKS);
    break;

  case "violation":
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_VIOLATION, OAA_AE_SIDEBAR.MSG_COUNT_VIOLATIONS);
    break;
  
  case 'warning':
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_WARNING, OAA_AE_SIDEBAR.MSG_COUNT_WARNINGS);
    break;
    
  case 'hidden':
    str =  OAA_AE_SIDEBAR.MSG_COUNT_HIDDEN;
    break;

  case 'required':
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_REQ_RULE, OAA_AE_SIDEBAR.MSG_COUNT_REQ_RULES);
    break;

  case 'recommended':
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_REC_RULE, OAA_AE_SIDEBAR.MSG_COUNT_REC_RULES);
    break;

  case 'total':
    str = getMessage(count, OAA_AE_SIDEBAR.MSG_COUNT_TOTAL_RULE, OAA_AE_SIDEBAR.MSG_COUNT_TOTAL_RULES);
    break;


  default:
    break;  
  }

  util.addElement(node_tr, 'td', count.toString(), style, 'presentation');
  util.addElement(node_tr, 'td', str, 'message', 'presentation');

};

/**
 * @function addPropertyHeader
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds a list item describing a count result
 *
 * @param {Object}  node   - DOM node of a table to add the header information  
 */
 
OAA_AE_SIDEBAR.util.addPropertyHeader = function (node) {

  var util = OAA_AE_SIDEBAR.util;

   var node_tr = util.addElement(node, 'tr', "");
   
   util.addElement(node_tr, 'th', "Attribute/Property");
   util.addElement(node_tr, 'th', "Value");
   util.addElement(node_tr, 'th', "Description");


};

/**
 * @function addPropertyData
 *
 * @memberOf OAA_AE_SIDEBAR.util
 *
 * @desc Adds a list item describing a count result
 *
 * @param {Object}  node   - DOM node of a table to add the header information  
 */
 
OAA_AE_SIDEBAR.util.addPropertyData = function (node, style, prop, value, desc) {

  var util = OAA_AE_SIDEBAR.util;

   var node_tr = util.addElement(node, 'tr', "", style);
   
   util.addElement(node_tr, 'td', prop, 'property');
   util.addElement(node_tr, 'td', value, 'value');
   util.addElement(node_tr, 'td', (desc === "") ? "  " : desc, 'description');

};

